package com.cskaoyan.gateway.controller.user;

import com.alibaba.fastjson.JSON;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author:tanxin Desc: 用户登录和验证登录
 * @date:2020/7/9 22:45
 */
@RestController
@RequestMapping("/user")
public class LoginController {
    @Reference(timeout = 3000,check = false)
    private ILoginService iUserLoginService;

    @Reference(timeout = 3000,check = false)
    private IKaptchaService iKaptchaService;

    /**
     * 用户登录
     * @param
     * @return
     */
    @Anoymous
    @PostMapping("/login")
    public ResponseData userLogin(@RequestBody Map<String,String> map, HttpServletRequest request,
                                  HttpServletResponse response){

        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        //校验验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        //1.设置uuid 通过Cookie传过来的
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        //2.设置验证码
        kaptchaCodeRequest.setCode(captcha);
        //开始校验
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
          return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        //校验用户名密码
        UserLoginResponse loginResponse = iUserLoginService.login(userLoginRequest);
        //根据响应体来校验
        if (SysRetCodeConstants.SUCCESS.getCode().equals(loginResponse.getCode())){
            //登录成功，设置Cookie
            Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN, loginResponse.getToken(),
                    "/", 60 * 60 * 24);
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            return new ResponseUtil<>().setData(loginResponse);
        }
        //登录失败
        return new ResponseUtil<>().setErrorMsg(loginResponse.getMsg());
    }

    /**
     * 校验token
     * @return
     */
    @GetMapping("/login")
    public ResponseData validToken(HttpServletRequest request){
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        Object o = JSON.parse(userInfo);
        return new ResponseUtil<>().setData(o);
    }
}
